package day3;

import java.util.Scanner;

import org.junit.Test;

public class array {
	@Test
	public void test01() {
		//int num = 3;
		byte num = 3;
		switch (num) {
		case 1:
			System.out.println("1");
			break;
		case 2:
			System.out.println("2");
			break;
		case 3:
			System.out.println("3");
			break;
		default:
			System.out.println("default");
			break;
	    }
    }

	@Test
	public void test02() {
		int [] array = new int[4];
		array[1] = 60;
		array[0] = 50;
		array[2] = 70;
		array[3] = 80;
		System.out.println(array.length);
		for (int i = 0; i <= array.length - 1; i++) {
			System.out.println(array[i]);
		}
		for (int i = array.length - 1; i >= 0; i--) {
			System.out.println(array[i]);
			
		}
	}
	
	
	
	//冒泡排序： {52, 20, 56, 14};
	public static void main(String[] args) {
		int[] array = {52, 20, 56, 14};
		for (int i = 1; i < array.length; i++) {
			for (int j = 0; j < array.length -i; j++) {
				if (array[j] < array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
			
		}
		for (int j = 0; j < array.length; j++) {
			System.out.println(array[j]);
		}
	}
	
	
	//int[] array = {23, 56, 8, 99, 2};
	//求数组最大值和最小值
	//MAX
	@Test
	public void test03() {
		int[] array  = {23, 56, 8, 99, 2};
		//假设array[0]即数组第一个数为最大值；
		int max = array[0];
		//for循环
		for (int i = 0; i < array.length; i++) {
			//若成立，假设是错的；
			if (max < array[i]) {
				//将最大值赋予新的；
				max = array[i];
			}
		}
		//输出最大值
		System.out.println(max);
	}
	
	
	//MIN
	@Test
	public void test04() {
		int[] array = {23, 56, 78, 89, 88, 89};
		int min = array[0];
		for(int i = 0; i < array.length; i++) {
			if (min > array[i]) {
				min = array[i];
			}
		}
		System.out.println("min:" + min);
	}
	
	//奇数与偶数在数组中计算出现的次数；
	@Test
	public void test05() {
		int evenCount = 0;
		int oddCount = 0;
		int[] array = {0, 2, 5, 8, 88, 745, 788, 879, 8484655, 45, 55, 85858};
		for (int i = 0; i < array.length; i++) {
			if(array[i] % 2 == 0) {
				evenCount++;
			}
			else {
				oddCount++;
			}
		}
		System.out.println("evenCount:" + evenCount);
		System.out.println("oddCount:" + oddCount);
	}
	
	// 求出上面数组中0-9分别出现的次数（有一点难度）

	// int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
	@Test
	public void test06() {
		int[] array={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
		int[] counts = new int[10];
		for (int i = 0; i < array.length; i++) {
			switch(array[i]) {
			case 0:
				counts[0]++;
				break;
			case 1:
				counts[1]++;
				break;
			case 2:
				counts[2]++;
				break;
			case 3:
				counts[3]++;
				break;
			case 4:
				counts[4]++;
				break;
			case 5:
				counts[5]++;
				break;
			case 6:
				counts[6]++;
				break;
			case 7:
				counts[7]++;
				break;
			case 8:
				counts[8]++;
				break;
			case 9:
				counts[9]++;
				break;
			default:
				break;
				
			}
		}
		for (int i = 0; i < counts.length; i++) {
			System.out.println(i + "出现的次数" + counts[i]);
		}
	}
	
	
	@Test
	public void test07() {
		for (int i = 100; i <= 200; i++) {
			boolean isSuShu = true;
			for (int j = 2; j < i; j++) {
				if (i % j == 0) {
					isSuShu = false;
					break;
					
				}
			}
			if (isSuShu == true) {
				System.out.println(i + "是素数");
				
			}
		}
	
	}
	
	
	@Test
	public void test08() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串：");
		String str = scanner.next();
		char[] array = str.toCharArray();
		//先反证 假设是回文 即 boolean isHuiWen = true；
		//反证法：假设是回文，遍历找到任何一个不是回文的即停止并输出“不是回文”；
		//结束当前循环，若发现boolean仍为true则输出'是回文'
		boolean isHuiWen = true;
		for (int i = 0 ; i < array.length/2; i++) {
			if(array[i] != array[array.length - 1 - i]) {
				isHuiWen = false;
				break;
				
			}
			
		} 
		
		if(isHuiWen == true) {
			System.out.println("是回文");
		}else {
			 System.out.println("bu是回文");
		}
	}
	
	
	@Test
	public void test09() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串：");
		//nextLine()输入的字符串里可以有空格
		String str = scanner.nextLine();
		char[] array = str.toCharArray();
		int letterCount = 0;
		int numberCount = 0;
		int blankCount = 0;
		int otherCount = 0;
		for (int i = 0; i < array.length; i++) {
			if((array[i] >= 'a' && array[i] <= 'z')||(array[i] >= 'A' && array[i] <= 'Z' )) {
				letterCount++;
			}
			else if(array[i] >= 0 && array[i] <= 9) {
				numberCount++;
			}
			else if(array[i] == ' ') {
				blankCount++;
			}
			else {
				otherCount++;
			}
		}
		System.out.println("英文字母有：" + letterCount);
		System.out.println("数字有：" + numberCount);
		System.out.println("空格有：" + blankCount);
		System.out.println("其他字符有：" + otherCount);
	}
	@Test
	public void test16() {
		int[] array = {23,4,56,78,99,22,67};
		for(int i = 1; i < array.length; i++) {
			for (int j = 0; j < array.length - i; j++) {
				if (array[j] < array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
					
				}
			}
	     }
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + "\t");
			
		}
	}
	
	@Test
	public void test18() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个字符串：");
		String str = scanner.next();
		char[] array = str.toCharArray();
		boolean isHuiWen = true;
		for (int i = 0; i < array.length; i++) {
			if (array[i] != array[array.length - 1 - i]) {
				isHuiWen = false;
				break;
			}
		}
		if (isHuiWen == true) {
			System.out.println("是回文");
			
		}else {
			
			System.out.println("不是回文");
		}
	}
}
